package org.i1510.joeone.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.i1510.common.bean.PageBean;
import org.i1510.common.utils.IDGenerator;
import org.i1510.common.utils.StringUtils;
import org.i1510.common.utils.web.ResultMapper;
import org.i1510.common.utils.web.WebUtils;
import org.i1510.joeone.dao.IJoeoneVoteCcDao;
import org.i1510.joeone.po.JoeoneVoteCc;
import org.i1510.joeone.service.IJoeoneVoteCcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "/admin/joeoneVoteCc")
public class JoeoneVoteCcController {
    private static final Logger logger = LoggerFactory.getLogger(JoeoneVoteCcController.class);
    @Autowired
    private IJoeoneVoteCcService iJoeoneVoteCcService;
    @Autowired
    private IJoeoneVoteCcDao iJoeoneVoteCcDao;
    
    private ResultMapper mapper;
    /**
    *@Author Rambo
    *@Desc：
    *@param request
    *@param joeoneVoteCc
    *@param errors
    *@return TODO
    *@Version  JoeoneVoteCcController.java,v 1.1 2015年8月27日 上午11:21:54 
    */
    @RequestMapping(value = "/saveShops", method = RequestMethod.POST)
    @ResponseBody
    public String saveShops(HttpServletRequest request,@Valid @ModelAttribute("joeoneVoteCc") JoeoneVoteCc joeoneVoteCc,BindingResult errors) {
        
        if(errors.hasErrors()){            
            mapper = WebUtils.getMapper4Errors(errors);
        }else{
        	String[] shops = joeoneVoteCc.getJshopcode().split(",");
        	for (int i = 0; i < shops.length; i++) {
        		JoeoneVoteCc cc = new JoeoneVoteCc();
        		cc.setDocStatus("10");
        		cc.setJdjr(joeoneVoteCc.getJdjr());
        		cc.setJdjt(joeoneVoteCc.getJdjt());
        		cc.setJnum(joeoneVoteCc.getJnum());
        		cc.setJshopcode(shops[i]);
        		cc.setJimg(joeoneVoteCc.getJimg());
                mapper = iJoeoneVoteCcService.save(cc,request);
			}
        }
        
        return mapper.toJson();
    }
    /**
    *@Author Rambo
    *@Desc：保存对象
    *@param joeoneVoteCc
    *@param errors
    *@return TODO
    *@Version  JoeoneVoteCcController.java,v 1.1 2013-11-15 下午4:20:56 
    */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String save(HttpServletRequest request,@Valid @ModelAttribute("joeoneVoteCc") JoeoneVoteCc joeoneVoteCc,BindingResult errors) {
        
        if(errors.hasErrors()){            
            mapper = WebUtils.getMapper4Errors(errors);
        }else{
            joeoneVoteCc.setDocStatus("10");
            mapper = iJoeoneVoteCcService.save(joeoneVoteCc,request);
        }
        
        return mapper.toJson();
    }
    /**
    *@Author Rambo
    *@Desc：删除对象
    *@param docId
    *@param errors
    *@return TODO
    *@Version  JoeoneVoteCcController.java,v 1.1 2014-9-2 下午3:38:17 
    */
    @RequestMapping(value = "/deleteX", method = RequestMethod.POST)
    @ResponseBody
    public String deleteX(@Valid @ModelAttribute("docId") String docId,BindingResult errors) {
        
        if(errors.hasErrors()){            
            mapper = WebUtils.getMapper4Errors(errors);
        }else{
            mapper = iJoeoneVoteCcService.delete(docId);
        }
        
        return mapper.toJson();
    }
    /**
    *@Author Rambo
    *@Desc：查看对象
    *@param docId
    *@param errors
    *@return TODO
    *@Version  JoeoneVoteCcController.java,v 1.1 2014-9-2 下午3:39:35 
    */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    @ResponseBody
    public String view(@Valid @ModelAttribute("docId") String docId,BindingResult errors) {
        ResultMapper mapperRtn = ResultMapper.generateFailure();
        if(errors.hasErrors()){            
            mapperRtn = WebUtils.getMapper4Errors(errors);
        }else{
            JoeoneVoteCc joeoneVoteCc = iJoeoneVoteCcService.findById(docId);
            mapperRtn = ResultMapper.generateSuccess();
            mapperRtn.appendResult("data", joeoneVoteCc);
        }        
        return mapperRtn.toJson();
    }
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public String list(@ModelAttribute("fromDate") String fromDate,@ModelAttribute("toDate") String toDate,@Valid @ModelAttribute("joeoneVoteCc") JoeoneVoteCc joeoneVoteCc,@Valid @ModelAttribute("pageBean") PageBean pageBean,BindingResult errors) {
        ResultMapper mapperRtn = ResultMapper.generateFailure();
        if(errors.hasErrors()){            
            mapperRtn = WebUtils.getMapper4Errors(errors);
        }else{
            Map conditions = new HashMap();
            conditions.put("fromDate", fromDate);
            conditions.put("toDate", toDate);
            List  joeoneVoteCcs = iJoeoneVoteCcService.getPage(joeoneVoteCc, pageBean,conditions);
            int totalCount =  iJoeoneVoteCcService.getPageCount(joeoneVoteCc,conditions);
            Map map = new HashMap();
            map.put("pageNo", pageBean.getPageNo());
            map.put("pageSize", pageBean.getPageSize());
            map.put("totalCount", totalCount);
            map.put("listData",joeoneVoteCcs);
            
            mapperRtn = ResultMapper.generateSuccess();
            mapperRtn.appendResult("data", map);
        }        
        return mapperRtn.toJson();
    }
    /**
    *@Author Rambo
    *@Desc：状态修改
    *@param request
    *@param docId
    *@param docStatus
    *@param errors
    *@return TODO
    *@Version  CateController.java,v 1.1 2014-9-11 上午10:06:04 
    */
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @ResponseBody
    public String updateStatus(HttpServletRequest request,@Valid @ModelAttribute("docId") String docId,@Valid @ModelAttribute("docStatus") String docStatus,BindingResult errors) {
        
        if(errors.hasErrors()){            
            mapper = WebUtils.getMapper4Errors(errors);
        }else{
            if(!StringUtils.isStringEmpty(docId)){
                JoeoneVoteCc joeoneVoteCc = new JoeoneVoteCc();
                
                joeoneVoteCc.setDocId(docId);
                joeoneVoteCc.setDocStatus(docStatus);
                mapper = iJoeoneVoteCcService.save(joeoneVoteCc,request);
            }else{
                mapper = ResultMapper.generateFailure();
                mapper.appendResult("messages", "无法找到该ID!");
            }
            
        }
        
        return mapper.toJson();
    }
}